package com.j2009.springbootproject.web;

import com.j2009.springbootproject.entity.ResponseResult;
import com.j2009.springbootproject.entity.User;
import com.j2009.springbootproject.service.UserService;
import com.j2009.springbootproject.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: UsersController
 * Description:
 * date: 2021/4/6 17:10
 *
 * @author wuyafeng
 * @version 1.0   softeem.com
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/login",method = RequestMethod.GET)
    public ResponseResult login(String username, String password){
        ResponseResult resp = new ResponseResult();
       User user =  userService.selectByUser(username,password);
       if(user!=null){
           //以前是写session ，现在返回token 到前台项目
          String token =  JWTUtils.createJWT(180,user);
           resp.setCode(0);
           resp.setData(token);
           resp.setMessage("登录成功");
       }else{
           resp.setCode(-1);
           resp.setMessage("登录失败");
       }

       return resp;
    }

    //未登录不能访问该list
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){

        System.out.println("用户列表查询");
        return "用户列表查询";

    }
}
